package  
{
	import flash.display.Sprite;
	
	/**
	 * ...
	 * @author patsup
	 * 		Super class for PirateLineup and PiratePlank.
	 * 	Handles removal of PirateUnits, moving from one to the other PirateLocation, and
	 * 	enabling/disabling the clicking of each of its PirateUnit buttons.
	 * 
	 **********	Potential issues:
	 * 		Will garbage collecting take care of PirateUnits removed by RemovePirates()?
	 * 
	 * *** public functions
	 * DisableButtons():void
	 * EnableButtons():void
	 * RemovePirates():void
	 * PirateMove(pirate:PirateUnit):void
	 * 
	 * GetPirateSum():int
	 * GetOtherLocation():PirateLocation
	 * SetOtherLocation(pirateLoc:PirateLocation):void
	 * 
	 */
	public class PirateLocation extends Sprite 
	{
		protected var pirates:Vector.<PirateUnit>;
		
		protected const LOC_PIRATE_LINEUP:int = 0;		//define the locations as integers in case there are more locations
		protected const LOC_PIRATE_PLANK:int = 1;
		
		private var otherPirateLoc:PirateLocation;		//lineup's other is plank and vice versa
		
		public function PirateLocation() 
		{
			mouseChildren = true;
			
			pirates = new Vector.<PirateUnit>();
		}
		
		//DisableButtons()
		//disable all the pirateunit buttons from being clicked
		public function DisableButtons():void
		{
			for each (var _pirate:PirateUnit in pirates)
			{
				_pirate.DisableButton();
			}
		}
		//EnableButtons()
		//enable all the pirateunit buttons to being clicked
		public function EnableButtons():void
		{
			for each (var _pirate:PirateUnit in pirates)
			{
				_pirate.EnableButton();
			}
		}
		
		//RemovePirates()
		//Remove all pirates from location (lineup or plank) - update vector and display list
		public function RemovePirates():void
		{
			while (pirates.length > 0) {
				var _p:PirateUnit = pirates.pop();	//Remove this pirate from vector
				_p.DisableButton();					//Remove its event listener
				removeChild(_p);					//Remove this pirate from display list
			}
			//Set the pirateLoc for pirate to signal that it's removed? No, not unless we're ever going to use that pirate again
		}
		
		//PirateMove()
		//Moves pirate from lineup to plank or vice versa, depending on location
		//Update vectors and display lists.
		//Since each PirateUnit knows its location as well as "the other", then it can
		//be moved without passing in a PirateLocation
		public function PirateMove(pirate:PirateUnit):void
		{
			pirates.splice(pirates.lastIndexOf(pirate), 1);		//Remove this pirate from this vector of pirates
			otherPirateLoc.pirates.push(pirate);				//Add this pirate to the other vector of pirates
			
			removeChild(pirate);								//Remove from this display list
			otherPirateLoc.addChild(pirate);					//Add to other display list
			
			pirate.SwapLocations();								//Swaps the pirateLoc for pirate
		}
		
		//GetPirateSum()
		//Returns the total value of the pirates
		public function GetPirateSum():int
		{
			var _total:int = 0;
			for each (var _pirate:PirateUnit in pirates)
			{
				_total += _pirate.GetNumber();			//get the pirate's number and calculate running total
			}
			return _total;
		}
		//GetPirateValues()
		//Returns a vector<int> of the values of pirate
		public function GetPirateValues():Vector.<int>
		{
			var _pirateValues:Vector.<int> = new Vector.<int>;
			for each (var _pirate:PirateUnit in pirates)
			{
				_pirateValues.push(_pirate.GetNumber());
			}
			return _pirateValues;
		}
		
		//GetOtherLocation()
		//Returns the PirateLocation class of the other one (for plank, this returns the lineup, and vice versa)
		public function GetOtherLocation():PirateLocation
		{
			return otherPirateLoc;
		}
		
		//SetOtherLocation()
		//Set the PirateLocation class for the other one (set plank's other location to lineup, and vice versa)
		public function SetOtherLocation(pirateLoc:PirateLocation):void
		{
			otherPirateLoc = pirateLoc;
		}
		
	}

}